package com.bdc.activity;

import com.bdc.Var;
import com.bdc.R;
import com.bdc.util.ServiceUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

public class StartActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start);
	}

	@Override
	protected void onResume() {
		super.onResume();

		SharedPreferences preferences = getSharedPreferences(Var.preference.file_name, MODE_PRIVATE);
		String userName = preferences.getString(Var.preference.user_name, null);
		String password = preferences.getString(Var.preference.password, null);

		boolean isSetedUserInfo = userName != null && password != null;
		if (isSetedUserInfo) {
			String methodUrl = Var.service.login + "/" + userName + "/" + password;
			String result = ServiceUtils.invokeService(methodUrl);
			
			int code = Integer.parseInt(result);
			boolean isSuccess = code != Var.service.login_code_error && code != Var.service.login_code_server;
			
			if(isSuccess) {
				Intent intent = new Intent();
				intent.setClass(StartActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
				return;
			}
		}
		
		Intent intent = new Intent();
		intent.setClass(StartActivity.this, LoginActivity.class);
		startActivity(intent);
		finish();
	}
}
